package maiyatian.videolive.fragment.focus;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import maiyatian.videolive.IClickListener;
import maiyatian.videolive.IFragment;
import maiyatian.videolive.MainActivity;
import maiyatian.videolive.R;
import maiyatian.videolive.adapter.main.FocusAdapter;
import maiyatian.videolive.adapter.main.SeebackAdapter;
import maiyatian.videolive.application.IApp;
import maiyatian.videolive.bean.LiveBean;
import maiyatian.videolive.pulltorefresh.XListView;

/**
 * Created by 王中阳 on 2016/5/10.
 */
@ContentView(R.layout.fragment_focus)
public class FocusFragment extends IFragment implements XListView.IXListViewListener, IClickListener {
    public static final String TAG = "FocusFragment";
    MainActivity mycontext;

    XListView xlvSeebak;
    FocusAdapter adapter;
    SeebackAdapter seebackAdapter;
    List<LiveBean> datas;
    @ViewInject(R.id.xlvFocus) // TODO: 2016/5/11 头文件
            XListView xListView;

    public FocusFragment() {

    }

    public static FocusFragment newInstance() {
        FocusFragment fragment = new FocusFragment();
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mycontext = (MainActivity) context;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return x.view().inject(this, inflater, container);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViews();
        xListView.setPullLoadEnable(true);
        xListView.setXListViewListener(this);
    }

    // TODO: 2016/5/13 footer 的listview不能正常显示
    private void initViews() {
        View headerView = LayoutInflater.from(mycontext).inflate(R.layout.header_focus_live, null); //头文件
        View footerView = LayoutInflater.from(mycontext).inflate(R.layout.content_seeback, null);//footer
        xlvSeebak = (XListView) footerView.findViewById(R.id.xlvSeeback);
        LiveBean liveBean = new LiveBean();
        datas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            datas.add(liveBean);
        }
        seebackAdapter = new SeebackAdapter(mycontext, datas, this);
        adapter = new FocusAdapter(mycontext, datas, this);
        xListView.addHeaderView(headerView);
        xListView.addFooterView(footerView);
        xListView.setAdapter(adapter);
        xlvSeebak.setAdapter(seebackAdapter);
    }

    @Override
    public void onRefresh() {
        mHandler.sendMessage(mHandler.obtainMessage(102, datas));
    }

    @Override
    public void onLoadMore() {
        mHandler.sendMessage(mHandler.obtainMessage(102, datas));
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 101:
//                    adapter = new ListViewAdapter(activity, cache.generateLocalList());
//                    xlistview.setAdapter(adapter);
//                    //if(adapter != null)
//                    //	adapter.add_updat(cache.generateLocalList());
                    sendEmptyMessage(110);
                    break;
                case 102:
                    //加载数据
//                    cache.update((ArrayList<HolderInfo>) msg.obj);
//                    if (adapter != null) {
//                        adapter.add_updat(cache.generateLocalList());
//                    }
                    sendEmptyMessage(101);
                    break;
                case 103:
                    //加载数据
//                    cache.update_nextPullto((ArrayList<HolderInfo>) msg.obj);
//                    if (adapter != null) {
//                        adapter.add_updat(cache.generateLocalList());
//                    }
                    sendEmptyMessage(110);
                    break;
                case 110:
                    xListView.stopRefresh();
                    xListView.stopLoadMore();
                    xListView.setRefreshTime("刚刚");
                    break;
                default:
                    break;
            }

        }

        ;
    };

    @Override
    public void onItemClicked(int position) {
        IApp.getInstance().showToast(mycontext, position + "");
    }
}
